package com.zhangchuchu.neohealth.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangchuchu.neohealth.common.R;
import com.zhangchuchu.neohealth.entity.Notice;
import com.zhangchuchu.neohealth.service.NoticeService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Notice)控制层
 *
 * @author makejava
 * @since 2025-04-15 08:29:11
 */
@RestController
@RequestMapping("/notice")
public class NoticeController {
    @Resource
    private NoticeService noticeService;

    private static final String UPLOAD_DIR = "D:\\IdeaProjects\\neo-health\\src\\main\\resources\\static\\img";
    /**
     * 查询所有数据（不分页）
     */
    @GetMapping("/getList")
    public R<List<Notice>> selectList() {
        return R.success(noticeService.list());
    }

    /**
     * 分页查询
     */
    @GetMapping
    public R<Page<Notice>> selectAll(Page<Notice> page, Notice notice) {
        return R.success(noticeService.page(page, new QueryWrapper<>(notice)));
    }

    /**
     * 通过主键查询单条数据
     */
    @GetMapping("{id}")
    public R<Notice> selectOne(@PathVariable Serializable id) {
        return R.success(noticeService.getById(id));
    }

    /**
     * 新增数据
     */
    @PostMapping
    public R<Boolean> insert(@RequestBody Notice notice) {
        return R.success(noticeService.save(notice));
    }

    /**
     * 修改数据
     */
    @PutMapping
    public R<Boolean> update(@RequestBody Notice notice) {
        return R.success(noticeService.updateById(notice));
    }

    /**
     * 删除数据（批量）
     */
    @DeleteMapping
    public R<Boolean> delete(@RequestParam("idList") List<Integer> idList) {
        return R.success(noticeService.removeByIds(idList));
    }

    @GetMapping("getOne")
    public R<Notice> getLastNotice(){
        return R.success(noticeService.getLastNotice());
    }

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> uploadFile(@RequestParam("file") MultipartFile file) {
        Map<String, String> result = new HashMap<>();

        if (file.isEmpty()) {
            result.put("error", "No file uploaded");
            return result;
        }

        try {
            // 获取文件名并设置目标路径
            String originalFilename = file.getOriginalFilename();
            String filePath = UPLOAD_DIR + "/" + originalFilename;
            File dest = new File(filePath);

            // 创建父目录
            dest.getParentFile().mkdirs();
            file.transferTo(dest); // 保存文件

            // 返回文件路径
            result.put("path", "/img/" + dest.getName()); // 返回相对路径
        } catch (IOException e) {
            result.put("error", "File upload failed");
            e.printStackTrace();
        }

        return result;
    }
}
